package cn.edu.usst.cs.goldfish.booking.meetingroom.config;

import cn.edu.usst.cs.goldfish.booking.meetingroom.interceptor.AuthInterceptor;
import cn.edu.usst.cs.goldfish.booking.meetingroom.interceptor.KeepAliveInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final AuthInterceptor authInterceptor;
    private final KeepAliveInterceptor keepAliveInterceptor;
//    private final BanApiInterceptor banApiInterceptor;
//
//
//    private TemplatedErrandInterceptor templatedErrandInterceptor;
//    private TemplatedForumInterceptor templatedForumInterceptor;
//

    public WebConfig(
            AuthInterceptor authInterceptor,
            KeepAliveInterceptor keepAliveInterceptor
//            ,
//            BanApiInterceptor banApiInterceptor,
//            TemplatedErrandInterceptor templatedErrandInterceptor,
//            TemplatedForumInterceptor templatedForumInterceptor

    ) {

        this.authInterceptor = authInterceptor;
//        this.banApiInterceptor = banApiInterceptor;
//        this.templatedErrandInterceptor = templatedErrandInterceptor;
//        this.templatedForumInterceptor = templatedForumInterceptor;
        this.keepAliveInterceptor = keepAliveInterceptor;
    }


    /**
     * 指定用户上传资源的路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置上传文件的访问路径
        registry
                .addResourceHandler("/uploads/**")
                .addResourceLocations("file:uploads/");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 配置拦截器
        registry
                .addInterceptor(authInterceptor)
//                .addPathPatterns("/api/**")
                .excludePathPatterns(
                    "/api/auth/**",
                    "/api/bookings/**",
                    "/api/admin/**",
                    "/api/alipay/**",
                    "/api/**",
                    "/api/meetings/**",
                    "/admin/**"
                )
        ;
        registry.addInterceptor(keepAliveInterceptor)
                .addPathPatterns("/api/keep-alive/**");
    }

    /**
     * allow vue at <a href="https://localhost:5173">...</a>
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter());
    }

}